﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using BusinessRules;
using ToolUltity;
using System.Collections;

namespace BusinessLibary
{
    public class ChargeItemInfoFacade
    {
        public ChargeItemInfoFacade()
        {
            log = LogService.Createinstance("SystemManage");
        }

        private LogService log;//日志文件
        public DataSet GetChargeInfoByInhosID(string inhosid)
        {
            try
            {
                ChargeItemInfoRules rule = new ChargeItemInfoRules();
                return rule.GetChargeInfoByInhosID(inhosid);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }

        public DataSet BuildChargeInfoByInhosID(string inhosid)
        {
            try
            {
                DataSet chargeItemInfo = new DataSet();
                chargeItemInfo = GetChargeInfoByInhosID(inhosid);//获取病人费用明细表

                if (chargeItemInfo.Tables[0].Rows.Count<=0)
                {
                    return null;
                }
                DataSet ds = new DataSet();
                DataTable myDataTable = new DataTable();
                DataColumn myDataColumn;

                //指定自费列
                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.Boolean");
                myDataColumn.ColumnName = "STATE";
                myDataColumn.DefaultValue = false;
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "病人姓名";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "住院号";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "医保号";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "项目类别";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "费用类别";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "医嘱序号";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "执行时间";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "收费项目内码";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "项目中心编码";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "项目名称";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "单价";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "数量";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "剂型";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "规格";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "单位";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "医生代码";
                myDataTable.Columns.Add(myDataColumn);

                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "经办人";
                myDataTable.Columns.Add(myDataColumn);

                //指定自费字段
                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "SELFMONEY";
                myDataTable.Columns.Add(myDataColumn);


                string xmlb = string.Empty;//项目类别
                string fylb = string.Empty;//费用类别
                string xmybbm = string.Empty;//项目中心编码
                string shxmnm = string.Empty;//收费项目内码
                string xmmc = string.Empty;//收费项目名称
                log.Info("以获取收费明细，组织表！");
                for (int i = 0; i < chargeItemInfo.Tables[0].Rows.Count; i++)
			    {
                    DataRow row=chargeItemInfo.Tables[0].Rows[i];
    			    DataRow myDataRow;
				    myDataRow = myDataTable.NewRow();

                    if (Convert.ToDouble(row["ypsl"].ToString().Trim())==0)
                    {
                        continue;//数量为0                        
                    }
                    #region 判断项目的类别
                    if (!row["idm"].ToString().Trim().Equals("0"))
                    {
                        xmmc = GetHOSYP(row["idm"].ToString())["ypmc"].ToString();
                        log.Info("获取编号为:" + row["idm"].ToString() + "名称：" + xmmc);
                        DataRow centerYp = GetCenterYp(row["idm"].ToString());//医保中心药品对应编码
                        //log.Info("1--------------");
                        if (centerYp != null  )
                        {
                            //log.Info("2--------------");
                            if (centerYp["ITEMIDMED"].ToString().Trim().Equals(string.Empty))
                            {
                               // log.Info("3--------------");
                                log.Info(row["idm"].ToString() + "与医保数据中心没有对应的项目");
                                xmlb = "1";
                                fylb = "91";
                                xmybbm = "AAAAA";
                                shxmnm = row["idm"].ToString();
                            }else
                            {
                               // log.Info("4--------------");
                                xmlb = "1";
                                fylb = GetYpByItem(centerYp["ITEMIDMED"].ToString())["SFLB"].ToString();//费用类别
                                //log.Info("5--------------");
                                xmybbm = centerYp["ITEMIDMED"].ToString();
                                shxmnm = row["idm"].ToString();
                            }
                        }
                        else
                        {
                            return null;
                        }
                       // log.Info("完成");
                    }
                    else
                    {
                        log.Info("获取编号为:" + row["ypdm"].ToString());
                        xmmc = GetHOSZL(row["ypdm"].ToString())["name"].ToString();
                        log.Info("获取编号为:" + row["ypdm"].ToString() + "名称：" + xmmc);
                        DataRow centerItem = GetCenteritem(row["ypdm"].ToString());//收费项目中心编码
                        //log.Info("1--------------");
                        if (centerItem != null  )
                        {
                           // log.Info("2--------------");
                            if (centerItem["ITEMIDMED"].ToString().Trim().Equals(string.Empty))
                            {
                              //  log.Info("3--------------");
                                log.Info(row["ypdm"].ToString() + "与医保数据中心没有对应的项目");
                                xmlb = centerItem["PRE1"].ToString();
                                fylb = "91";
                                xmybbm = "AAAAA";
                                shxmnm = row["ypdm"].ToString();
                            }
                            else
                            {
                                //log.Info("4--------------中心编码：" + centerItem["ITEMIDMED"].ToString());
                                xmlb = centerItem["PRE1"].ToString();
                                fylb = GetZLByItem(centerItem["ITEMIDMED"].ToString())["SFLB"].ToString();
                              //  log.Info("5--------------");
                                xmybbm = centerItem["ITEMIDMED"].ToString();
                                shxmnm = row["ypdm"].ToString();
                            }
                        }
                        else
                        {
                            return null;
                        }
                        //log.Info("完成");
                    }

                    #endregion
                    //log.Info("设置完成");
                    myDataRow["病人姓名"] = row["hzxm"].ToString();
                    myDataRow["住院号"] = row["blh"].ToString();
                    myDataRow["医保号"] = row["MEDICARENO"].ToString();
                    myDataRow["项目类别"] = xmlb;
                    myDataRow["费用类别"] = fylb;
                    myDataRow["医嘱序号"] = row["xh"].ToString();//序号不唯一
                    myDataRow["执行时间"] = row["zxrq"].ToString().Replace(":","");
                    myDataRow["收费项目内码"] = shxmnm;
                    myDataRow["项目中心编码"] = xmybbm;
                    myDataRow["项目名称"] = xmmc;
                    myDataRow["单价"] = row["ypdj"].ToString();
                    myDataRow["数量"] = (Decimal.Round(Convert.ToDecimal(row["ypsl"].ToString())/Convert.ToDecimal(row["ykxs"].ToString()),4)).ToString();
                    myDataRow["剂型"] = "";
                    myDataRow["规格"] = row["ypgg"].ToString();
                    myDataRow["单位"] = row["ypdw"].ToString();
                    myDataRow["医生代码"] = row["ysdm"].ToString();
                    myDataRow["经办人"] = row["czyh"].ToString();
                    myDataRow["SELFMONEY"] = "0";//默认不指定自费 为0 指定自费为2
                    myDataRow["STATE"] = false;//默认为不选中
                    myDataTable.Rows.Add(myDataRow);
			    }
                ds.Tables.Add(myDataTable);


                //ds.WriteXml("bb.xml");
                return ChangeDataSet(ds);
            }
            catch (Exception e)
            {
                
                throw new Exception(e.Message,e);
            }
        }

        private DataSet ChangeDataSet(DataSet ds)
        {
            DataSet newData = new DataSet();
            newData = ds;
            DataSet dd = new DataSet();
            dd = ds.Clone();
            Hashtable tbl = new Hashtable();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                if (Convert.ToDouble(row["数量"].ToString()) < 0 && !tbl.ContainsKey(row["项目名称"].ToString() + row["收费项目内码"].ToString()))
                {
                    //AND 数量 ='"row[" + 数量"].ToString().Substring(1) + "'
                    DataRow[] rows = newData.Tables[0].Select("项目名称 ='" + row["项目名称"] + "' AND 收费项目内码 ='" + row["收费项目内码"]  +"'");
                    tbl.Add(row["项目名称"].ToString() + row["收费项目内码"].ToString(),"");
                    if (rows.Length > 0)
                    {
                        int rowNumber = 0;
                        decimal count=0;
                        for (int i = 0; i < rows.Length; i++)
                        {
                            //if (!tbl.ContainsKey(rows[i]["医嘱序号"].ToString()))
                            //{
                            //    tbl.Add(rows[i]["医嘱序号"].ToString(), "");
                            //    rowNumber = i;
                            //    break;
                            //}
                            count += Convert.ToDecimal(rows[i]["数量"].ToString());
                            
                        }
                        
                        if (count == 0)
                        {
                            //row.Delete();
                        }
                        else
                        {
                            
                            row.BeginEdit();
                            row["数量"] = count;
                            row.EndEdit();
                            dd.Tables[0].ImportRow(row);
                        }
            
                    }
                    continue;

                }
                if (tbl.ContainsKey(row["项目名称"].ToString() + row["收费项目内码"].ToString()))
                {
                    //row.Delete();
                    continue;
                }
                dd.Tables[0].ImportRow(row);
                
            }
            dd.WriteXml("bb.xml");
            return dd;
        }
        private string ConvertDateTime(string datetime)
        {
            return "";
        }
        private HisYPInfoFacade ypfacade = null;
        private HisZLInfoFacade zlfacade = null;
        private MIItemRelationZLFacade miitemRelationZLFacade = null;
        private MIItemRelationYPFacade miitemRelationYbFacade = null;

        //获取医院的药品收费项目名称
        private DataRow GetHOSYP(string idm)
        {
            
            if (ypfacade==null)
            {
                ypfacade = new HisYPInfoFacade();
            }
            DataSet ds = ypfacade.GetYPInfo(idm);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0];
            }
            return null;
        }

        //获取医院的诊疗项目名称
        private DataRow GetHOSZL(string ypdm)
        {
            
            if (zlfacade==null)
            {
                zlfacade = new HisZLInfoFacade();                    
            }

            DataSet ds = zlfacade.GetZLInfo(ypdm);
            if (ds.Tables[0].Rows.Count>0)
            {
                return ds.Tables[0].Rows[0];                
            }
            return null;
 
        }
        //获取药品项目对应的医保中心药品项目编码
        private DataRow GetCenterYp(string leechdomno)
        {
            
            if (miitemRelationYbFacade==null)
            {
                miitemRelationYbFacade = new MIItemRelationYPFacade();
            }
            DataSet ds = miitemRelationYbFacade.FindByPrimaryKey(leechdomno);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0];
            }
            return null;
        }
        //获取诊疗项目对应的医保中心项目中心编码
        private DataRow GetCenteritem(string itemId)
        {
            
            if (miitemRelationZLFacade==null)
            {
                miitemRelationZLFacade = new MIItemRelationZLFacade(); 
            }
            DataSet ds = miitemRelationZLFacade.FindByPrimaryKey(itemId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0];
            }
            return null;
        }

        //获取药品医保对应的信息
        private DataRow GetYpByItem(string itemId)
        {
            YBYPInfoFacade ybYpMLFacade = null;
            if (ybYpMLFacade==null)
            {
                ybYpMLFacade = new YBYPInfoFacade();
            }
            DataSet ds = ybYpMLFacade.FindByPrimaryKey(itemId);
            if (ds.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }
        //获取诊疗项目医保对应的信息
        private DataRow GetZLByItem(string itemId)
        {
            YBZLInfoFacade ybZLXMFacade = null;
            if (ybZLXMFacade==null)
            {
                ybZLXMFacade = new YBZLInfoFacade();
            }
            DataSet ds = ybZLXMFacade.FindByPrimaryKey(itemId);
            if (ds.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }




    }
}
